<?php
/**
 * 用户管理模块
 */
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\data\Pagination;
use app\models\EmailSystemEmail;
class UserManageController extends \app\base\Controller
{
    /**
     * 用户列表
     */
    public function actionIndex()
    {
        $data = [];
        $siteInfo = [];
        
        $defaultPageSize = 50;
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        
        // 查询网站相关的数据
        $formData = Yii::$app->request->$mothod();
       
       // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getSysEmailAccessSite();
        
        $siteInfo[0] = '网站';
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['site_id']] = $siteInfo_tmp_v['name'];
            }
        }
        
        
        
        // 当前用户所有有权限的站点结束
        $formData['siteInfo'] = $siteInfo;
      
        // 用户邮箱
        $formData['email'] = isset($formData['email']) ? $formData['email'] : '';
       
        // 时间相关
        $formData['startTime'] = isset($formData['startTime']) ? $formData['startTime'] : '';
        $formData['endTime'] = isset($formData['endTime']) ? $formData['endTime'] : '';
        
       
        // 当前搜索哪个网站的用户信息
        $formData['site_id'] = isset($formData['site_id']) ? $formData['site_id'] : 0;
        
        if ($formData['site_id']) {
            $flag = Yii::$app->authority->judgeSysEmailHaveSite($formData['site_id']);
            if (!$flag) {
                 $this->sysMsg('警告', '你的账号没有权限!',$params = [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
            }
        } 
        
        // 分页参数相关
        $page = Yii::$app->request->$mothod('page') ? Yii::$app->request->$mothod('page') : 1;
        $pageSize = Yii::$app->request->$mothod('pagesize') ? Yii::$app->request->$mothod('pagesize') : $defaultPageSize;
        $formData['page'] = $page;
        $formData['pageSize'] = $pageSize;
        
        
        
        
        // 保存用户结果数据
        $data_select = Yii::$app->usermanage->getUserList($formData);
        $totalCount = $data_select['totalCount'];
        
        // 分页相关
        $pages = new Pagination(['totalCount' =>$totalCount, 'pageSize' => $pageSize, 'defaultPageSize' => $pageSize]);
        $pageCount = $pages->getPageCount(); //页数

        $pageOther = ['totalCount' => $totalCount, 'pageCount' => $pageCount];
        // 渲染模版
        return $this->render('list',[
            'data' => $data_select['user_info'],
            'site' => $site,
            'pages' => $pages,
            // 'pageOther' => $pageOther,
            'formData' => $formData,
            // 'curPageIds' => $curPageIds,           
        ]);
    }
    
    /**
     * 获取当前用户的邮件
     */
    public function actionUserEmailList()
    {
        
       
        // 默认只是显示当年要的邮件
        $mothod = strtolower(Yii::$app->request->getMethod());
        $user_id = Yii::$app->request->$mothod('user_id');
        $site_name = Yii::$app->request->$mothod('site_name');
        $site = Yii::$app->request->$mothod('site');
        $site_id = Yii::$app->request->$mothod('site_id');
        
        // 查询网站相关的数据
        $formData = Yii::$app->request->$mothod();
        // 当前搜索哪个网站的用户信息
        $formData['site_id'] = isset($formData['site_id']) ? $formData['site_id'] : 0;
        $formData['startTime'] = isset($formData['startTime']) ? $formData['startTime'] : '';
        $formData['endTime'] = isset($formData['endTime']) ? $formData['endTime'] : '';
       
        
        if (empty($site_name)) {
            echo '缺少参数site_name';
            exit;
        }
        $moduleEmail = new EmailSystemEmail();
        
        $user_email_arr = $moduleEmail->getEmail($user_id,$site_name,$formData);
        
        // 处理数据
        if ($user_email_arr && is_array($user_email_arr)) {
            foreach ($user_email_arr as $user_email_key => $user_email_val) {
                
                // 邮件状态相关
                switch($user_email_val['status']){
                    case '0':
                        $status = '在未发送队列';
                        break;
                    case '1':
                        $status = '发送成功';
                        break;
                    case '2':
                        $status = '在重发队列';
                        break;
                    case '3':
                        $status = '发送失败';
                        break;
                    default:
                        $status = '未知状态';
                        break;
                        
                }
                $user_email_arr[$user_email_key]['status'] = $status;
                
                // 邮件发送时间相关
                if (isset($user_email_val['send_time']) && $user_email_val['send_time']) {
                   $user_email_arr[$user_email_key]['send_time'] = Yii::$app->systime->date($user_email_val['send_time']);
                } else {
                   $user_email_arr[$user_email_key]['send_time'] = ' ';
                }  
            }
        }
        
         // 渲染模版
        return $this->render('user-email-list',[
            'data' => $user_email_arr,
            'site' => $site,
            'site_name' => $site_name,
            'user_id' => $user_id,
            //'pages' => $pages,
            // 'pageOther' => $pageOther,
            'formData' => $formData,
            // 'curPageIds' => $curPageIds,           
        ]);
    }
    
    /**
     * 组装搜索页面的url地址
     */
    public function getUrl($formData) 
    {
        
    }
} 